//
//  UserDetailVC.m
//  TheProjectName
//
//  Created by 李劲成 on 16/5/23.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "UserDetailVC.h"
#import "ViewControllerOfLogin.h"
#import "AppDelegate.h"
#define MAIN_SCREEN [UIScreen mainScreen].bounds.size

#define COLOR [UIColor colorWithRed:0.09 green:0.30 blue:0.41 alpha:1.00]

#define VIEWOFUSER_HEIGHT viewOfUser.frame.size.height / 7.0

#define VIEWOFUSER_WIDTH viewOfUser.frame.size.width

@interface UserDetailVC ()

@property (nonatomic, retain) UIImageView *imageViewOfUser;

@property (nonatomic, retain) UILabel *labelOfUserName;
@property (nonatomic, retain) UILabel *labelOfUserNameDetails;
@property (nonatomic, retain) UILabel *labelOfSex;
@property (nonatomic, retain) UILabel *labelOfSexDetails;
@property (nonatomic, retain) UILabel *labelOfSpace;
@property (nonatomic, retain) UILabel *labelOfSpaceDetails;
@property (nonatomic, retain) UILabel *labelOfSign;
@property (nonatomic, retain) UILabel *labelOfSignDetails;

@property (nonatomic, retain) UIButton *buttonOfCancel;

@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) UIImageView *imageViewOfStart;

@end

@implementation UserDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startAnimation];
    
//    [self addBackImage];
    
    [self createSubviews];
}

#pragma mark - 添加背景
//- (void)addBackImage
//{
//    // 背景
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"7"ofType:@"png"];
//    
//    UIImage *image = [UIImage imageWithContentsOfFile:path];
//    
//    self.view.layer.contents = (id)image.CGImage;
//}

#pragma mark - 创建视图
- (void)createSubviews
{
    UIView *viewOfUser = [[UIView alloc] init];
    viewOfUser.frame = CGRectMake(30, MAIN_SCREEN.height / 5.0, MAIN_SCREEN.width - 60, MAIN_SCREEN.height / 2.0);
    [self.view addSubview:viewOfUser];
    viewOfUser.backgroundColor = [UIColor whiteColor];
    viewOfUser.layer.masksToBounds = YES;
    viewOfUser.layer.cornerRadius = 10.0f;
    viewOfUser.alpha = 0.7f;
    
    // 添加用户图片
    self.imageViewOfUser = [[UIImageView alloc] init];
    self.imageViewOfUser.frame = CGRectMake(MAIN_SCREEN.width / 2.0 - VIEWOFUSER_HEIGHT, MAIN_SCREEN.height / 5.0 - VIEWOFUSER_HEIGHT, VIEWOFUSER_HEIGHT * 2, VIEWOFUSER_HEIGHT * 2);
    self.imageViewOfUser.image = [UIImage imageNamed:@"1111.jpg"];
    self.imageViewOfUser.layer.masksToBounds = YES;
    self.imageViewOfUser.layer.cornerRadius = VIEWOFUSER_HEIGHT;
    [self.view addSubview:self.imageViewOfUser];
    
    //添加用户名Label
    self.labelOfUserName = [[UILabel alloc] init];
    self.labelOfUserName.frame = CGRectMake(20, VIEWOFUSER_HEIGHT, VIEWOFUSER_WIDTH / 3.0 - 20, VIEWOFUSER_HEIGHT);
    [viewOfUser addSubview:self.labelOfUserName];
    self.labelOfUserName.text = @"用户名:";
    self.labelOfUserName.textColor = COLOR;
    self.labelOfUserName.font = [UIFont systemFontOfSize:15 weight:2];
    
    // 添加用户名详情Label
    self.labelOfUserNameDetails = [[UILabel alloc] init];
    self.labelOfUserNameDetails.frame = CGRectMake(20 + VIEWOFUSER_WIDTH / 3.0, VIEWOFUSER_HEIGHT, VIEWOFUSER_WIDTH / 3.0 * 2.0 - 40, VIEWOFUSER_HEIGHT);
    self.labelOfUserNameDetails.textColor = COLOR;
    [viewOfUser addSubview:self.labelOfUserNameDetails];
    self.labelOfUserNameDetails.text = [EMClient sharedClient].currentUsername;
    self.labelOfUserNameDetails.font = [UIFont systemFontOfSize:15 weight:2];
    
    // 添加用户View
    UIView *viewOfLineUser = [[UIView alloc] init];
    viewOfLineUser.frame = CGRectMake(20, VIEWOFUSER_HEIGHT +VIEWOFUSER_HEIGHT + 3, VIEWOFUSER_WIDTH - 40, 1);
    [viewOfUser addSubview:viewOfLineUser];
    viewOfLineUser.backgroundColor = COLOR;
    
    // 添加性别Label
    self.labelOfSex = [[UILabel alloc] init];
    self.labelOfSex.frame = CGRectMake(20, VIEWOFUSER_HEIGHT * 3, VIEWOFUSER_WIDTH / 3.0 - 20, VIEWOFUSER_HEIGHT);
    [viewOfUser addSubview:self.labelOfSex];
    self.labelOfSex.text = @"性别:";
    self.labelOfSex.textColor = COLOR;
    self.labelOfSex.font = [UIFont systemFontOfSize:15 weight:2];
    
    // 添加性别详情Label
    self.labelOfSexDetails = [[UILabel alloc] init];
    self.labelOfSexDetails.frame = CGRectMake(20 + VIEWOFUSER_WIDTH / 3.0, VIEWOFUSER_HEIGHT * 3, VIEWOFUSER_WIDTH / 3.0 * 2.0 - 40, VIEWOFUSER_HEIGHT);
    self.labelOfSexDetails.textColor = COLOR;
    [viewOfUser addSubview:self.labelOfSexDetails];
    self.labelOfSexDetails.font = [UIFont systemFontOfSize:15 weight:2];
    
    // 添加性别View
    UIView *viewOfLineSex = [[UIView alloc] init];
    viewOfLineSex.frame = CGRectMake(20, VIEWOFUSER_HEIGHT * 3 +VIEWOFUSER_HEIGHT + 3, VIEWOFUSER_WIDTH - 40, 1);
    [viewOfUser addSubview:viewOfLineSex];
    viewOfLineSex.backgroundColor = COLOR;
    
    // 添加地区Label
    self.labelOfSpace = [[UILabel alloc] init];
    self.labelOfSpace.frame = CGRectMake(20, VIEWOFUSER_HEIGHT * 4, VIEWOFUSER_WIDTH / 3.0 - 20, VIEWOFUSER_HEIGHT);
    [viewOfUser addSubview:self.labelOfSpace];
    self.labelOfSpace.text = @"地区:";
    self.labelOfSpace.textColor = COLOR;
    self.labelOfSpace.font = [UIFont systemFontOfSize:15 weight:2];
    
    // 添加地区详情Label
    self.labelOfSpaceDetails = [[UILabel alloc] init];
    self.labelOfSpaceDetails.frame = CGRectMake(20 + VIEWOFUSER_WIDTH / 3.0, VIEWOFUSER_HEIGHT * 4, VIEWOFUSER_WIDTH / 3.0 * 2.0 - 40, VIEWOFUSER_HEIGHT);
    self.labelOfSpaceDetails.textColor = COLOR;
    [viewOfUser addSubview:self.labelOfSpaceDetails];
    self.labelOfSexDetails.font = [UIFont systemFontOfSize:20 weight:2];
    
    // 添加地区View
    UIView *viewOfLineSpace = [[UIView alloc] init];
    viewOfLineSpace.frame = CGRectMake(20, VIEWOFUSER_HEIGHT * 4 +VIEWOFUSER_HEIGHT + 3, VIEWOFUSER_WIDTH - 40, 1);
    [viewOfUser addSubview:viewOfLineSpace];
    viewOfLineSpace.backgroundColor = COLOR;
    
    // 添加个性签名Label
    self.labelOfSign = [[UILabel alloc] init];
    self.labelOfSign.frame = CGRectMake(20, VIEWOFUSER_HEIGHT * 5, VIEWOFUSER_WIDTH / 3.0 - 20, VIEWOFUSER_HEIGHT);
    [viewOfUser addSubview:self.labelOfSign];
    self.labelOfSign.text = @"个性签名:";
    self.labelOfSign.textColor = COLOR;
    self.labelOfSign.font = [UIFont systemFontOfSize:15 weight:2];
    
    // 添加个性签名详情Label
    self.labelOfSignDetails = [[UILabel alloc] init];
    self.labelOfSignDetails.frame = CGRectMake(20 + VIEWOFUSER_WIDTH / 3.0, VIEWOFUSER_HEIGHT * 5, VIEWOFUSER_WIDTH / 3.0 * 2.0 - 40, VIEWOFUSER_HEIGHT);
    self.labelOfSignDetails.textColor = COLOR;
    [viewOfUser addSubview:self.labelOfSignDetails];
    self.labelOfSignDetails.font = [UIFont systemFontOfSize:20 weight:2];
    
    // 添加个性签名View
    UIView *viewOfLineSign = [[UIView alloc] init];
    viewOfLineSign.frame = CGRectMake(20, VIEWOFUSER_HEIGHT * 5 +VIEWOFUSER_HEIGHT + 3, VIEWOFUSER_WIDTH - 40, 1);
    [viewOfUser addSubview:viewOfLineSign];
    viewOfLineSign.backgroundColor = COLOR;
    
    // 添加注销按钮
    self.buttonOfCancel = [UIButton buttonWithType:UIButtonTypeSystem];
//    self.buttonOfCancel.frame = CGRectMake(MAIN_SCREEN.width / 2.0 - 300 / 2.0, MAIN_SCREEN.height - 200 , 300, 40);
    self.buttonOfCancel.frame = CGRectMake(MAIN_SCREEN.width / 2.0 - (MAIN_SCREEN.width - 50) / 2.0, MAIN_SCREEN.height / 5.0 + VIEWOFUSER_HEIGHT * 7 + 30, MAIN_SCREEN.width - 50, VIEWOFUSER_HEIGHT);
    self.buttonOfCancel.layer.masksToBounds = YES;
    self.buttonOfCancel.layer.borderWidth = 1.0f;
    self.buttonOfCancel.layer.borderColor = [UIColor whiteColor].CGColor;
    self.buttonOfCancel.layer.cornerRadius = 8.0f;
    [self.buttonOfCancel setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.buttonOfCancel addTarget:self action:@selector(handleCancel:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonOfCancel];
    self.buttonOfCancel.titleLabel.font = [UIFont systemFontOfSize:22];
    [self.buttonOfCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

#pragma mark - 退出登录事件
- (void)handleCancel:(UIButton *)button
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         [[EMClient sharedClient] logout:YES];
    });
    ViewControllerOfLogin *login = [[ViewControllerOfLogin alloc]init];
    AppDelegate *appdelegate =[[UIApplication sharedApplication] delegate];
    appdelegate.window.rootViewController = login;
    
}

#pragma mark - 星星动画
- (void)startAnimation
{
    self.image = [UIImage imageNamed:@"start"];
    
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(handleStart) userInfo:nil repeats:YES];
}

#pragma mark - 计时器方法
- (void)handleStart
{
    self.imageViewOfStart = [[UIImageView alloc] initWithImage:self.image];
    [self.view addSubview:self.imageViewOfStart];
    
    // 设置透明度
    self.imageViewOfStart.alpha = 0.8;
    // 星星的大小
    int size = arc4random() % 10;
    // 开始y坐标
    int  startY = round(arc4random() % 1000);
    // 最终y坐标
    int finshY = startY + 600;
    // 速度
    double speed = 1 / round(arc4random() % 100) + 0.3;
    
    //星星开始的大小和位置
    self.imageViewOfStart.frame = CGRectMake(-10, startY - 500, size, size);
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:3 *speed];
    
    // 星星最后消失的坐标
    self.imageViewOfStart.frame = CGRectMake(1000.0, finshY, size, size);
    
    [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    self.imageViewOfStart = nil;
    [self.imageViewOfStart removeFromSuperview];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
      self.navigationItem.leftBarButtonItem = BARBUTTONIMAGE(@"aligh_left_27.291428571429px_1189116_easyicon.net", self, @selector(presentLeftMenuViewController:));
    self.navigationItem.title = @"个人信息";
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
